<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\STeacherInfo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TeacherController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('教师')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('教师')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('教师')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('教师')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new STeacherInfo);

		$grid->id('Id')->sortable();
		$grid->name('姓名');
		$grid->phone('电话');
		$grid->email('邮箱');
		$grid->headimg('头像')->image();
		$grid->type('类型')->display(function ($type) {
			return config("svs.teacher_type." . $type);
		});
		$grid->tags_zh_cn('标签')->display(function ($v) {
			// config('svs.course_type'))->implode(',
			$s = '';
			foreach ($v as $i) {
				$s .= config('svs.course_type.' . $i) . ',';
			}
			return trim($s, ',');
		});
		// $grid->tags_en('标签(英)');
		$grid->certification_zh_cn('证书')->implode(',');
		// $grid->certification_en('证书(英)');
		$grid->desc_zh_cn('中文简介')->limit(30);
		// $grid->desc_en('英文简介')->limit(30);
		$grid->desc_voice('音频简介')->display(function ($desc) {
			return "<a href='{$desc}' target='_block'></a>";
		});
		$grid->created_at('创建时间')->sortable();
		$grid->updated_at('更新时间');

		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(STeacherInfo::findOrFail($id));

		$show->id('Id');
		$show->name('姓名');
		$show->phone('电话');
		$show->emial('邮箱');
		$show->headimg('头像')->image();
		$show->type('类型');
		$show->tags_zh_cn('标签')->as(function ($v) {
			$s = '';
			foreach ($v as $i) {
				$s .= config('svs.course_type.' . $i) . ',';
			}
			return trim($s, ',');
		});
		$show->certification_zh_cn('证书')->as(function ($v) {
			return trim(implode(',', $v), ',');
		});

		$show->desc_zh_cn('中文简介');
		// $show->desc_en('英文简介');
		$show->desc_voice('音频简介')->file();
		$show->created_at('创建时间');
		$show->updated_at('更新时间');
		// $show->deleted_at('Deleted at');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new STeacherInfo);

		$form->text('name', '姓名')->rules('required');
		$form->text('phone', '电话')->rules('required');
		$form->email('email', '邮箱')->rules('required');
		$form->password('password', '密码')->rules('required');
		$form->image("headimg", "头像")->rules('required');
		$form->select("type", "教师类型")->options(config("svs.teacher_type"))->rules('required')->default(1);
		// $form->multipleSelect('tags_en', '标签(英)')->options(config("svs.course_type"));
		$form->multipleSelect('tags_zh_cn', '标签')->options(config("svs.course_type"))->rules('required');
		$form->tags('certification_en', '证书(英)')->help("多个证书用英文逗号隔开");
		$form->tags('certification_zh_cn', '证书(中)')->help("多个证书用英文逗号隔开");

		$form->textarea('desc_en', '简介(英)');
		$form->textarea('desc_zh_cn', '简介(中)');
		$form->file('desc_voice', '音频简介');
		$form->saving(function (Form $form) {
			if ($form->password && $form->model()->password != $form->password) {
				$form->password = bcrypt($form->password);
			}
		});

		$form->saved(function (Form $form) {
			dispatch(new \App\Jobs\NewTeacherAddSchedule($form->model()->id));
		});

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});

		return $form;
	}
}
